<template>
  <div class="login-vue">
    <div class="login-container">
      <div class="login-content">
        <div class="login-title">{{ title }}</div>
        <div class="login-input">
          <input type="text" name="username" v-model="username" autocomplete="off"/>
          <span class="placeholder" :class="{fixed: username != '' && username != null}">用户名</span>
        </div>
        <div class="login-password login-input">
          <input type="password" name="password" v-model="password" autocomplete="off"/>
          <span class="placeholder" :class="{fixed: password != '' && password != null}">密码</span>
        </div>
        <div class="login-input">
          <input type="text" name="verify" v-model="verify" @keyup.enter="submit" autocomplete="off" style="width: 150px"/>
          <span class="placeholder" :class="{fixed: verify != '' && verify != null}">验证码</span>
          <img style="width: 100px;height:40px;cursor: pointer;" :src="verifyUrl" @click="updateVerify"/>
        </div>
        <div class="buttonDiv">
          <Button :loading="loading" block color="primary" size="l" @click="submit">登录</Button>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import {ajax} from 'framework'
import {mapActions} from 'Vuex'
export default {
  name: "Login",
  data: function () {
    return {
      title: 'Easy Manager',
      username:'',
      password: '',
      verify:'',
      loading: false,
      verifyUrl:'captcha?t='+new Date().getTime()
    };
  },
  methods: {
    ...mapActions(['loadUserInfo']),
    updateVerify(){
      this.verifyUrl='captcha?t='+new Date().getTime();
      this.verify=''
    },
    submit: function () {
      if (!this.password) {
        this.$Message("密码不能为空");
        return;
      }
      if (!this.verify) {
        this.$Message("验证码不能为空");
        return;
      }
      if (this.verify.length!=5) {
        this.$Message("验证码长度为5位");
        return;
      }
      this.loading = true;
      ajax.post("system/login", {username:this.username,password: this.password,verify:this.verify}).then((r) => {
        this.loadUserInfo().then(res=>{
          this.loading = false;
        }).catch(e=>{
          this.updateVerify()
          this.loading = false;
        })
      }).catch((e) => {
        //更新验证码
        this.updateVerify()
        this.loading = false;
      })
    }
  }
};
</script>
<style>
.login-vue {
  text-align: center;
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  background: #f7f8fa;
}

.login-vue .login-container {
  width: 320px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.login-vue .login-container .login-content {
  letter-spacing: 2px;
  background: #FFF;
  padding: 70px 30px 20px;
  box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.06);
  border-radius: 3px;
  box-sizing: border-box;
}

.login-vue .login-container .login-content > div {
  margin: 30px 0;
}

.login-vue .login-container .login-content > div.login-input {
  position: relative;
}

.login-vue .login-container .login-content > div.login-input .placeholder {
  position: absolute;
  color: #7e7e7e;
  top: 6px;
  font-size: 16px;
  transition: all .2s;
  left: 0;
  pointer-events: none;
}

.login-vue .login-container .login-content > div.login-input input {
  font-size: 16px;
  padding: 8px 0;
  height: 40px;
  width: 100%;
  border: none;
  border-radius: 0;
  border-bottom: 1px solid #d3d3d3;
  box-shadow: inset 0 0 0 1000px #fff;
  outline: none;
  box-sizing: border-box;
  transition: .3s;
  font-weight: 200;
}

.login-vue .login-container .login-content > div.login-input input:focus {
  border-bottom-color: #3788ee;
  box-shadow: inset 0 0 0 1000px #fff;
}

.login-vue .login-container .login-content > div.login-input input:focus + .placeholder,
.login-vue .login-container .login-content > div.login-input .placeholder.fixed {
  font-size: 13px;
  top: -16px;
}

.login-vue .login-container .login-content > div.login-input input:-webkit-autofill + .placeholder {
  font-size: 13px;
  top: -16px;
}

.login-vue .login-container .login-content > div.login-title {
  font-size: 30px;
  color: #3a3a3a;
  line-height: 1;
  margin: -16px 0px 40px;
  font-weight: 200;
}

.login-vue .login-container .login-content > .buttonDiv {
  margin-top: 45px;
}

.login-vue .login-container .login-content > .buttonDiv .h-btn {
  padding: 12px 0;
  font-size: 18px;
  opacity: .8;
  border-radius: 3px;
  background: #3788ee;
  border-color: #3788ee;
}

.login-vue .login-container .login-content > .buttonDiv .h-btn:hover {
  opacity: 1;
  background: #3788ee;
  border-color: #3788ee;
}

.login-vue .login-container .copyright {
  letter-spacing: 1px;
  margin-top: 30px;
  color: #7e7e7e;
}

.login-vue .login-container .copyright a {
  color: #7e7e7e;
}

</style>
